﻿using System;
using System.Collections.Generic;
using System.Text;
using ElectronicCommerce.Framework.Collections;
using ElectronicCommerce.Framework.Diagnostics;

namespace ElectronicCommerce.Framework.Utility
{
    /// <summary>
    /// 文件合并结果。
    /// </summary>
    public class FileMergeResult
    {
        #region [ Fields ]

        private List<string> filesToMerge = new List<string>();
        private List<string> filesMerged = new List<string>();
        private string fileContentMerged = string.Empty;
        private KeyedCollection<FileMergeFailReason> fileMergeFailReasons =
            new KeyedCollection<FileMergeFailReason>();

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filesToMerge"></param>
        public FileMergeResult(List<string> filesToMerge)
        {
            this.filesToMerge = filesToMerge;
        }

        #endregion

        #region [ Properties ]

        /// <summary>
        /// 存在文件被合并。
        /// </summary>
        public bool HasFileMerged
        {
            get { return filesMerged.Count > 0; }
        }

        /// <summary>
        /// 所有文件都已经合并。
        /// </summary>
        public bool AllFilesMerged
        {
            get { return filesToMerge.Count == filesMerged.Count; }
        }

        /// <summary>
        /// 要合并的文件。
        /// </summary>
        public List<string> FilesToMerge
        {
            get { return this.filesToMerge; }
        }

        /// <summary>
        /// 正常合并的文件。
        /// </summary>
        public List<string> FilesMerged
        {
            get { return this.filesMerged; }
        }

        /// <summary>
        /// 合并的文件内容。
        /// </summary>
        public string FileContentMerged
        {
            get { return this.fileContentMerged; }
            set { this.fileContentMerged = value; }
        }

        /// <summary>
        /// 合并失败的文件及原因。
        /// </summary>
        public KeyedCollection<FileMergeFailReason> FileMergeFailReasons
        {
            get { return this.fileMergeFailReasons; }
        }

        #endregion

        #region [ Methods ]

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="mergeFailReason"></param>
        public void AddFileMergeFailReason(string fileName, string mergeFailReason)
        {
            AddFileMergeFailReason(fileName, mergeFailReason, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="mergeFailReason"></param>
        public void AddFileMergeFailReason(string fileName, Exception innerException)
        {
            Check.Argument.IsNotNull("innerException", innerException);

            AddFileMergeFailReason(fileName, innerException.Message, innerException);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="mergeFailReason"></param>
        public void AddFileMergeFailReason(string fileName, string mergeFailReason, Exception innerException)
        {
            FileMergeFailReason failReason = new FileMergeFailReason();
            failReason.FileName = fileName;
            failReason.MergeFailReason = mergeFailReason;
            failReason.InnerException = innerException;

            this.fileMergeFailReasons.Add(failReason);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (FileMergeFailReason failReason in this.fileMergeFailReasons)
            {
                if (sb.Length > 0)
                {
                    sb.Append(",");
                }

                sb.AppendLine(string.Format("{0}:{1}", failReason.FileName, failReason.MergeFailReason));
            }

            return base.ToString();
        }

        #endregion
    }
}
